package com.llsydn.a04customswitch;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author lilinshen
 * @title 自定义开关控件
 * @description 请填写相关描述
 * @date 2018/10/15 16:13
 */
public class MyToggleButton extends View implements View.OnClickListener {
    private Bitmap backgroundBitmap;
    private Bitmap slidingBitmap;
    private int slidLeftMax;
    private int slidLeft;
    private Paint paint;
    private boolean isOpen = false;
    private float startX;
    private float lastX;
    private boolean isEnableClick = false;

    /**
     * 在布局文件中使用该类，需要用这个构造方法实例类。
     *
     * @param context
     * @param attrs
     */
    public MyToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        paint = new Paint();
        paint.setAntiAlias(true);
        backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.switch_background);
        slidingBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.slide_button);
        slidLeftMax = backgroundBitmap.getWidth() - slidingBitmap.getWidth();
        setOnClickListener(this);
    }

    /**
     * 视图的测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(backgroundBitmap.getWidth(), backgroundBitmap.getHeight());
    }

    /**
     * 绘制
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(backgroundBitmap, 0, 0, paint);
        canvas.drawBitmap(slidingBitmap, slidLeft, 0, paint);
    }

    /**
     * 设置点击监听
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (isEnableClick) {
            isOpen = !isOpen;
            if (isOpen) {
                slidLeft = slidLeftMax;
            } else {
                slidLeft = 0;
            }
            invalidate(); //导致onDraw执行
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //记录按下的坐标
                lastX = event.getX();
                isEnableClick = true;
                break;
            case MotionEvent.ACTION_MOVE:
                //得到结束值
                float endX = event.getX();
                //计算偏移量
                float distanceX = endX - startX;
                slidLeft += distanceX;
                //屏蔽非法值
                if (slidLeft < 0) {
                    slidLeft = 0;
                } else if (slidLeft > slidLeftMax) {
                    slidLeft = slidLeftMax;
                }
                //刷新
                invalidate();
                //数据还原
                startX = event.getX();
                if (Math.abs(endX - lastX) > 5) {
                    //滑动，点击事件设置为false
                    isEnableClick = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                if (!isEnableClick) {
                    if (slidLeft > slidLeftMax / 2) {
                        isOpen = true;
                    } else {
                        isOpen = false;
                    }
                    if (isOpen) {
                        slidLeft = slidLeftMax;
                    } else {
                        slidLeft = 0;
                    }
                    invalidate(); //导致onDraw执行
                }
                break;
        }
        return true;
    }
}
